package org.zjx.window;

import com.sun.jna.platform.win32.WinDef;
import lombok.Data;
import org.zjx.entity.Rect;

import java.util.Objects;

@Data
public class WindowResource {
    public enum ResourceState {
        ALLOCATED,    // 已分配使用中
        PRE_ALLOCATED, // 预分配(创建中)
        AVAILABLE      // 可用
    }

    private WinDef.HWND hwnd;
    private final Rect rect;
    private volatile ResourceState state = ResourceState.AVAILABLE;
    private volatile long lastUpdateTime = System.currentTimeMillis();

    public WindowResource(WinDef.HWND hwnd, Rect rect) {
        this.hwnd = hwnd;
        this.rect = rect;
    }

    public synchronized boolean tryPreAllocate() {
        if (this.state == ResourceState.AVAILABLE) {
            this.state = ResourceState.PRE_ALLOCATED;
            this.lastUpdateTime = System.currentTimeMillis();
            return true;
        }
        return false;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        WindowResource that = (WindowResource) o;
        return Objects.equals(rect, that.rect);
    }

    @Override
    public int hashCode() {
        return Objects.hash(rect);
    }
}